package raft

import (
	"go.uber.org/zap"
	"xin/raft/pb"
)

type MemoryTransport struct {
	consumerCh chan *RPCReq
	logger     *zap.Logger
}

func NewMemoryTransport(logger *zap.Logger) *MemoryTransport {
	return &MemoryTransport{
		consumerCh: make(chan *RPCReq, 1),
		logger:     logger,
	}
}

func (m *MemoryTransport) Init() error {
	return nil
}
func (m *MemoryTransport) Shutdown() error {
	return nil
}

func (m *MemoryTransport) RequestVote(req *pb.RequestVote, endpoint Endpoint, resp *pb.RequestVoteResp) error {
	resp.VoteGranted = true
	resp.Term = req.Term

	return nil
}

// AppendEntries 发送日志条目
func (m *MemoryTransport) AppendEntries(req *pb.AppendEntries, endpoint Endpoint, resp *pb.AppendEntriesResp) error {
	resp.Success = true
	resp.Term = req.Term

	return nil
}

// Consumer 节点接收其他节点的 RPC 请求
func (m *MemoryTransport) Consumer() chan *RPCReq {
	return m.consumerCh
}
